package com.bosssoft.trainee.nontax.controller;

import com.bosssoft.trainee.nontax.annotation.PassToken;
import com.bosssoft.trainee.nontax.annotation.UserLoginToken;
import com.bosssoft.trainee.nontax.common.result.CodeMsg;
import com.bosssoft.trainee.nontax.common.result.Result;
import com.bosssoft.trainee.nontax.entity.dto.RoleDto;
import com.bosssoft.trainee.nontax.entity.dto.TreeNode;
import com.bosssoft.trainee.nontax.entity.vo.RoleVo;
import com.bosssoft.trainee.nontax.service.jwt.TokenService;
import com.bosssoft.trainee.nontax.service.PermissionService;
import com.bosssoft.trainee.nontax.service.RoleService;
import com.bosssoft.trainee.nontax.util.mapper.dtoVo.RoleDtoVo;
import com.bosssoft.trainee.nontax.util.mapper.poDto.RolePoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Slf4j
@Controller
public class RoleController {
    @Autowired
    RoleService roleService;

    @Autowired
    TokenService tokenService;

    @Autowired
    PermissionService permissionService;

    @Autowired
    RoleDtoVo roleDtoVo;

    @Autowired
    RolePoDto rolePoDto;


    /**
     * @Description: 获取所有角色
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.List<com.bosssoft.trainee.nontax.entity.vo.RoleVo>>
     * @Author: Luo WenLong
     * @Date: 2022/6/25 8:27
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/role/getAll")
    public Result<List<RoleVo>> getAllRoles() {
        return Result.success((roleService.queryAllByRole(new RoleDto())));
    }

    /**
     * @Description: 获取所有的角色用 tree node 的形式返回
     * @param: null
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.List<com.bosssoft.trainee.nontax.entity.dto.TreeNode>>
     * @Author: Luo WenLong
     * @Date: 2022/6/25 8:27
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping("/role/getAllInTreeNodeTemplate")
    public Result<List<TreeNode>> getAllInTreeNodeTemplate() {
        return Result.success(roleService.queryAllInTreeNodeTemplate());
    }

    /**
     * @Description: 获取async router
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.Map<java.lang.String,java.util.List<java.lang.String>>>
     * @Author: Luo WenLong
     * @Date: 2022/6/22 9:09
     */
    @RequestMapping("/role/getAsyncRouterRoles")
    @ResponseBody
    @PassToken
    public Result<Map<String, List<String>>> getAsyncRouterRoles() {
        log.info("get AsyncRouterRoles");
        Map<String,List<String>> tagRoles = permissionService.getAsyncRouterPermission();
        return Result.success(tagRoles);
    }

    /**
     * @Description:
     * @param pageName 页面名称
     * @return: com.bosssoft.trainee.nontax.common.result.Result<java.util.Map<java.lang.String,java.util.List<java.lang.String>>>
     * @Author: Luo WenLong
     * @Date: 2022/6/22 9:09
     */
    @RequestMapping("/role/getUserPageRoles")
    @ResponseBody
    @PassToken
    public Result<Map<String, List<String>>> getUserPageRoles(@RequestParam String pageName) {
        log.info("get role of page by pageName:"+pageName);
        Map<String,List<String>> tagRoles = permissionService.getPermissionByPageName(pageName);
        if(tagRoles.isEmpty()){
            return Result.error(CodeMsg.NOT_FIND_DATA);
        }
        return Result.success(tagRoles);
    }

    /**
     * @Description: 添加角色方法
     * @param roleVo 本次添加的角色vo
     * @return: com.bosssoft.trainee.nontax.common.result.Result<com.bosssoft.trainee.nontax.entity.vo.RoleVo>
     * @Author: Luo WenLong
     * @Date: 2022/6/22 9:08
     */
    @UserLoginToken
    @RequestMapping("/role/addRole")
    @ResponseBody
    public Result<RoleVo> addRole(@RequestBody RoleVo roleVo){
        log.info("add role by roleVo: "+roleVo);
        if (roleVo.getRoleName() == null || roleVo.getRoleName().equals(""))
            return Result.error(CodeMsg.ROLE_ADD_FAILURE);

        RoleVo result = roleService.insert(roleDtoVo.vo2dto(roleVo));

        if (result == null)
            return Result.error(CodeMsg.ROLE_ADD_FAILURE);
        return Result.success(result);
    }

    @UserLoginToken
    @RequestMapping("/role/deleteRole")
    @ResponseBody
    public Result<String> deleteRole(@RequestParam(name = "roleId") Integer roleId){
        log.info("delete role by roleId: " + roleId);
        String canDelete = roleService.canDeleteRole(roleId);
        if (canDelete != null){
            return Result.error(CodeMsg.builder().retCode(999).message(canDelete).build());
        }
        roleService.deleteById(roleId);
        return Result.success();
    }
}
